package com.jic.purchase.service.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.purchase.constant.enums.DeptTypeEnum;
import com.jic.purchase.model.entity.SupplierRangeProduct;
import com.jic.purchase.mapper.SupplierRangeProductMapper;
import com.jic.purchase.model.vo.request.RangeProductRequest;
import com.jic.purchase.model.vo.request.SupplierRangeProductRequest;
import com.jic.purchase.model.vo.response.SupplierRangeProductResponse;
import com.jic.purchase.service.SupplierRangeProductService;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.Page;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 供应商范围商品
 * @author : 
 * @email : 
 * @since : 2020-09-20 06:58:04
 * @version : v1.0.0
 */
@Service
@Slf4j
public class SupplierRangeProductServiceImpl implements SupplierRangeProductService{

    @Autowired
    private SupplierRangeProductMapper supplierRangeProductMapper;

    @Override
    public long insert(SupplierRangeProduct supplierRangeProduct) {
        long flag = supplierRangeProductMapper.insert(supplierRangeProduct);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return supplierRangeProductMapper.delete(id);
    }

    @Override
    public long update(SupplierRangeProduct supplierRangeProduct) {
        return supplierRangeProductMapper.update(supplierRangeProduct);
    }

    @Override
    public SupplierRangeProduct queryByPrimaryKey(Long id){
        return supplierRangeProductMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<SupplierRangeProduct> query(SupplierRangeProduct supplierRangeProduct){
        return supplierRangeProductMapper.query(supplierRangeProduct);
    }

    @Override
    public PageResult<SupplierRangeProduct> queryPage(SupplierRangeProduct supplierRangeProduct, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<SupplierRangeProduct> list = supplierRangeProductMapper.queryPage(startIndex, pageQuery.getLimit(), supplierRangeProduct);
        long count = supplierRangeProductMapper.queryPageCount(supplierRangeProduct);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public PageResult<SupplierRangeProductResponse> querySupplierRangeProductList(SupplierRangeProductRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        //供应商平台则
        if (request.getAdminOperate() != null && request.getAdminOperate().getDeptType() != null
                && request.getAdminOperate().getDeptType().equals(DeptTypeEnum.SUPPLIER_TYPE.getType())){
            request.setSupplierId(request.getAdminOperate().getBusinessId());
        }
        List<SupplierRangeProductResponse> list = supplierRangeProductMapper.querySupplierRangeProductList(request);
        PageInfo pageInfo = new PageInfo(list);
        PageResult<SupplierRangeProductResponse> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return result;
    }

    @Override
    public Boolean batchInsert(RangeProductRequest request) {
        if (request == null || CollectionUtils.isEmpty(request.getRequests())){
            log.error("batchInsert param is null");
            return false;
        }
        List<SupplierRangeProduct> supplierRangeProducts = request.getRequests().stream().map(item -> {
            SupplierRangeProduct rangeProduct = new SupplierRangeProduct();
            BeanUtils.copyProperties(item, rangeProduct);
            rangeProduct.setDeleteFlag(0);
            rangeProduct.setCreateTime(new Date());
            rangeProduct.setDeptType(request.getAdminOperate() != null ? request.getAdminOperate().getDeptType().getCode() : null);
            rangeProduct.setDeptId(request.getAdminOperate() != null ? request.getAdminOperate().getDeptId() : null);
            rangeProduct.setCreator(request.getAdminOperate() != null ? request.getAdminOperate().getDeptId() : null);
            if(request.getAdminOperate() != null && request.getAdminOperate().getDeptType().getCode().equals(DeptTypeEnum.SUPPLIER_TYPE.getType())){
                rangeProduct.setSupplierId(request.getAdminOperate().getBusinessId());
            }
            return rangeProduct;
        }).filter(item -> item != null).collect(Collectors.toList());
        supplierRangeProductMapper.batchInsert(supplierRangeProducts);
        return true;
    }
}